package org.shj.demo;

import com.hankcs.hanlp.classification.classifiers.IClassifier;
import com.hankcs.hanlp.classification.classifiers.NaiveBayesClassifier;
import com.hankcs.hanlp.classification.models.NaiveBayesModel;
import com.hankcs.hanlp.corpus.io.IOUtil;

/**
 * @author Shen Huang Jian
 * @date 2020-11-05 13:25
 */
public class EmotionDemo {
    private static String modelPath = "D:\\tmp\\sentiment2.bin";

    public static void main(String[] args) throws Exception{
        trainModel();
        NaiveBayesModel model = (NaiveBayesModel) IOUtil.readObjectFrom(modelPath);

        IClassifier classifier = new NaiveBayesClassifier(model); // 创建分类器，更高级的功能请参考IClassifier的接口定义
        //classifier.train("D:\\project\\zhenfawei\\data\\hanlp\\data\\corpus\\sentiment");
        String text = "感觉很伤心。";
        System.out.printf("《%s》 情感极性是 【%s】\n", text, classifier.classify(text));
    }

    private static void trainModel() throws Exception{
        IClassifier classifier = new NaiveBayesClassifier(); // 创建分类器，更高级的功能请参考IClassifier的接口定义
        classifier.train("D:\\project\\zhenfawei\\data\\hanlp\\data\\corpus\\sentiment");

        NaiveBayesModel model = ((NaiveBayesClassifier) classifier).getNaiveBayesModel();
        IOUtil.saveObjectTo(model, modelPath);
    }
}
